package com.cloudptt.api.product.operator;

import android.content.Context;
import android.os.RemoteException;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.pjsip.api.ImsProfile;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.McpttLoginInfo;

public class SysOperator extends SipOperator
{
	private static final String THIS_FILE = "SysOperator";
	private SysConfig config = null;

	public SysOperator(Context ctx,SysConfig config)
	{
		super(ctx);
		this.config = config;

//		int resolving = config.getVideoConfig().getVideoResolving();
//		this.setVideoAttr(resolving);
	}

	public int login()
	{
		Log.e(THIS_FILE, "login");
		McpttLoginInfo mcpttLoginInfo = config.getMcpttLoginInfo();

		McIdInfo userInfo = mcpttLoginInfo.getMcIdInfo();
		String num = userInfo.getAddrId();
		String pwd = userInfo.getPwd();

		try
		{
			//logout();
			login(num, pwd);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			return CauseCont.OperateCheck_Sys_OperateException;
		}

		return CauseCont.OperateCheck_Suc;
	}

	public int login(String num,String pwd) throws RemoteException
	{
		Log.e(THIS_FILE, "login num = " + num + " pwd = ******");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			Log.e(THIS_FILE, "login result = " + result);
			return result;
		}

		this.config.setUserInfoBeforLogin(num, pwd);

		ImsProfile profile =  this.config.getMcpttLoginInfo().toImsProfile();
		Log.e(THIS_FILE, "login profile = " + profile);
		service.addAccount(profile,pwd);
		return CauseCont.OperateCheck_Suc;
	}

	public int refreshAccount()throws RemoteException
	{
		Log.e(THIS_FILE, "refreshAccount");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			Log.e(THIS_FILE, "refreshAccount result = " + result);
			return result;
		}
		service.refreshAccount();
		return CauseCont.OperateCheck_Suc;
	}

	public int restart()
	{
		Log.d(THIS_FILE, "restart");
		//		int result = checkFirst();
		//		if(CauseCont.OperateCheck_Suc != result)
		//		{
		//			return result;
		//		}
		try
		{
			if(service != null) {
				service.askThreadedRestart();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int logout()
	{
		Log.d(THIS_FILE, "logout");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.removeAllAccounts();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int setVideoAttr(final int videoResolving)
	{
		Log.d(THIS_FILE, "setVideoAttr videoResolving = " + videoResolving);
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.setVideoAttr(videoResolving);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

//	public int addBuddy(int mcType,String mcId,String requestLineUri,String xCapStr)
//	{
//		Log.d(THIS_FILE, "addBuddy mcId = " + mcId + " requestLineUri = " + requestLineUri + " xCapStr = " + xCapStr);
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try 
//		{
//			service.addBuddy(mcType,mcId,requestLineUri,xCapStr);
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}

	public int delAccount()
	{
		Log.d(THIS_FILE, "delAccount");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.delCurAccount();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int sendPublishPocSetting(int mcType,String accessToken )
	{
		Log.d(THIS_FILE, "sendPublishPocSetting mcType = " + mcType);
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.sendPublishPocSetting(mcType,accessToken);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int subcribeCms(int mcType,String accessToken)
	{
		Log.d(THIS_FILE, "subcribeCms mcType = " + mcType);
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.subcribeCms(mcType,accessToken);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int subcribeGms(String[] groupArray,String accessToken)
	{
		Log.d(THIS_FILE, "subcribeGms groupArray = " + groupArray);
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.subcribeGms(groupArray,accessToken);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	//	public int sendChPwdSysMsg(String oldPwd,String pwd) throws RemoteException
//	{
//		Log.d(THIS_FILE, "sendChPwdSysMsg oldPwd = " + oldPwd + " pwd = " + pwd);
//		String msgStr = "";//sysMsgCreator.modifyPasswordMsg(oldPwd, pwd);
//		return sendSysMsg(msgStr);
//	}
//
//	public int sendQryNewUserMsg(String num) throws RemoteException
//	{
//		Log.d(THIS_FILE, "sendQryNewUserMsg num = " + num);
//		String msgStr = "";//sysMsgCreator.qryNewUserMsg(num);
//		return sendSysMsg(msgStr);
//	}
//
	public int sendSysMsg(String msgStr)
	{
		Log.d(THIS_FILE, "sendSysMsg msgStr = " + msgStr);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		try
		{
			service.sendSystemMessage(msgStr);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int sendCmdMsg(String peerMcpttId,String cmdType)
	{
		Log.d(THIS_FILE, "sendSysMsg peerMcpttId = " + peerMcpttId + " cmdType = " + cmdType);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		try
		{
			service.sendCmdMessage(peerMcpttId,cmdType);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

//	public int startHandShakeTimer()
//	{
//		Log.d(THIS_FILE, "startHandShakeTimer");
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try {
//			service.startHandShakeTimer("");
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
//
//	public int stoptHandShakeTimer()
//	{
//		Log.d(THIS_FILE, "stoptHandShakeTimer");
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try {
//			service.stopHandShakeTimer();
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
}
